package nachos.threads;
import nachos.machine.*;
import java.util.Random;

public class CommunicatorTest {

	//calls the speak function, outputs before and after calling
	public static class speaks implements Runnable
	{
		public speaks(Communicator co, int i) {c = co; k = i;}
	
		public void run() 
		{
			System.out.println("    " + k + " speaking...");
			c.speak(k);
			System.out.println("    " + k + " done speaking");
		}
		private Communicator c;
		private int k;
	}
	
	//calls the listen function, outputs before and after calling
	public static class listens implements Runnable
	{
		public listens(Communicator co) {c = co;}
		
		public void run() 
		{
			System.out.println("    listening...");
			word = c.listen();
			System.out.println("    Done listening: " + word);
		}
		private Communicator c;
		private int word;
	}

    public static void runTest() 
	{
		System.out.println("  Communicator testing begins");

	
    	Communicator comm = new Communicator();   // Create the communicator that objects use to communicate 

		//creates two speakers and two listeners, then forks them.
		(new KThread(new speaks(comm, 1))).fork();  
		(new KThread(new listens(comm))).fork();
		(new KThread(new speaks(comm, 3))).fork();
		KThread last = (new KThread(new listens(comm))); //need a variable name for the last thread
		last.fork();									 //so that we can join to it
		last.join();
	
		System.out.println("  Communicator testing ends");
	
    }
}
